<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/js/themes/default/easyui.css" />
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/js/themes/icon.css" />
    <script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/jquery-1.8.3.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/extEasyUI.js"></script>
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/css/default.css" />
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/css/extEasyUIIcon.css" />
	<script type="text/javascript">
		var roleTree;
		
		jQuery(function(){
			roleTree = jQuery('#treegrid').treegrid({
			    url:'<%=request.getContextPath() %>/role/getRoleList',
			    title:'角色列表',
			    idField:'id',
			    treeField:'name',
			    parentField:'pid',
			    rownumbers:true,
			    toolbar:'#toolbar',
			    columns:[[
			        {field:'name',title:'角色名称'},
			        {field:'role',title:'上级角色',formatter:function(v,r,i){
			        	if (v){
							return v['name'];
						}
						return '';
			        }},
			        {field:'enable',title:'是否启用',formatter:function(v,r,i){
			        	return v=='true' ? "是" : "否";
			        }},
			        {field:'description',title:'描述'}
			    ]]
			});
			
		})
		
		function delRole(){
			var selectNode = jQuery("#treegrid").treegrid("getSelected");
			
			if (selectNode == null){
				parent.$.messager.alert('提示', '请点击一行选择您要删除的角色。', 'info');
				return false;
			}
			
			var children = jQuery("#treegrid").treegrid("getChildren",selectNode.id);
			
			if (children.length > 0){
				parent.$.messager.alert('提示', '请先删除该角色下的子级角色。', 'info');
				return false;
			}
			
			parent.$.messager.confirm('询问', '您是否要删除当前角色？', function(b) {
				if (b) {
					openProgress();
					$.post('<%=request.getContextPath() %>/role/delRole', {
						id : selectNode.id
					}, function(data) {
						parent.$.messager.progress('close');
						
						if (data.result) {
							parent.$.messager.alert('提示', '删除成功', 'info');
							roleTree.treegrid('reload');
						}
					}, 'JSON');
				}
			});
		}
		
		
		function addRole(){
			parent.$.modalDialog({
				title : '创建角色',
				width : 440,
				height : 220,
				href : '<%=request.getContextPath() %>/role/addRole',
				buttons:[{
					text : '保存',
					iconCls:'icon-ok',
					handler:function(){
						parent.$.modalDialog.roleTree = roleTree;
						parent.$.modalDialog.handler.find('#form').submit();
					}
				},{
					text : '取消',
					iconCls:'icon-cancel',
					handler:function(){
						parent.$.modalDialog.handler.dialog('close');
					}
				}]
			});
		}
		
		
		function editRole(){
			var selectNode = jQuery("#treegrid").treegrid("getSelected");
			
			if (selectNode == null){
				parent.$.messager.alert('提示', '请点击一行选择您要修改的角色。', 'info');
				return false;
			}
			
			parent.$.modalDialog({
				title : '修改角色',
				width : 440,
				height : 240,
				href : '<%=request.getContextPath() %>/role/editRole?id='+selectNode.id,
				buttons:[{
					text : '保存',
					iconCls:'icon-ok',
					handler:function(){
						parent.$.modalDialog.roleTree = roleTree;
						parent.$.modalDialog.handler.find('#form').submit();
					}
				},{
					text : '取消',
					iconCls:'icon-cancel',
					handler:function(){
						parent.$.modalDialog.handler.dialog('close');
					}
				}]
			});
		}
		
		function fpyh(){
			var selectNode = jQuery("#treegrid").treegrid("getSelected");
			
			if (selectNode == null){
				parent.$.messager.alert('提示', '请点击一行选择您要操作的角色。', 'info');
				return false;
			}
			
			parent.$.modalDialog({
				title : '分配用户',
				width : 950,
				height : 380,
				href : '<%=request.getContextPath() %>/role/fpyh?id='+selectNode.id,
				buttons:[{
					text : '取消',
					iconCls:'icon-cancel',
					handler:function(){
						parent.$.modalDialog.handler.dialog('close');
					}
				}]
			});
		}
		
		function grantPerm(){
			var selectNode = jQuery("#treegrid").treegrid("getSelected");
			
			if (selectNode == null){
				parent.$.messager.alert('提示', '请点击一行选择您要操作的角色。', 'info');
				return false;
			}
			
			var dialog = parent.$.modalDialog({
				title : '角色授权',
				width : 550,
				height : 570,
				href : '<%=request.getContextPath() %>/role/grantPerm?id='+selectNode.id,
				buttons:[{
					text : '保存',
					iconCls:'icon-ok',
					handler:function(){
						var nodes = parent.$.modalDialog.handler.find('#permTree').tree('getChecked', ['checked','indeterminate']);
						var roleId = parent.$.modalDialog.handler.find('#roleId').val();
						
						var ids = [];
						jQuery.each(nodes,function(i,n){
							ids.push(nodes[i]["id"]);
						});
						jQuery.post("<%=request.getContextPath() %>/role/saveRolePerm",{ids:ids.join(","),roleId:roleId},function(data){
							if (data){
								parent.$.messager.alert('提示', '保存成功。', 'info');
							}
						},"json");
					}
				},{
					text : '取消',
					iconCls:'icon-cancel',
					handler:function(){
						parent.$.modalDialog.handler.dialog('close');
					}
				}]
			});
		}
		
	</script>
	
  </head>
  <body>
  
  	<!-- grid -->
  	<table id="treegrid"></table>
    
    <!-- 工具条 -->
    <div id="toolbar">
    	<shiro:hasPermission name="Role:add"> 
    		<a href="#" class="easyui-linkbutton" iconCls="icon_group_add" plain="true" onclick="addRole()">创建角色</a>
    	</shiro:hasPermission>
    	<shiro:hasPermission name="Role:update"> 
    		<a href="#" class="easyui-linkbutton" iconCls="icon_group_edit" plain="true" onclick="editRole()">修改角色</a>
    	</shiro:hasPermission>
    	<shiro:hasPermission name="Role:delete"> 
    		<a href="#" class="easyui-linkbutton" iconCls="icon_group_delete" plain="true" onclick="delRole()">删除角色</a>
    	</shiro:hasPermission>
    	<shiro:hasPermission name="Role:grantUser"> 
    		<a href="#" class="easyui-linkbutton" iconCls="icon_group_key" plain="true" onclick="fpyh();">分配用户</a>
    	</shiro:hasPermission>
        
        <shiro:hasPermission name="Role:grantPerm"> 
        	<a href="#" class="easyui-linkbutton" iconCls="database_key" plain="true" onclick="grantPerm();">角色授权</a>
        </shiro:hasPermission>
        
        <a onclick="roleTree.treegrid('reload');" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon_refresh'">刷新</a>
    </div>
    
  </body>
</html>
